package com.jackchan.homework.sysw.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jackchan.homework.Constant;
import com.jackchan.homework.common.utils.DateUtils;
import com.jackchan.homework.sysw.dto.RecipeDto;
import com.jackchan.homework.sysw.dto.RecipeNeedsDto;
import com.jackchan.homework.sysw.entity.Recipe;
import com.jackchan.homework.sysw.entity.RecipeNeeds;
import com.jackchan.homework.sysw.repository.RecipeDao;
import com.jackchan.homework.sysw.repository.RecipeNeedsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by ch-hui on 15/12/24.
 */
@Service
public class RecipeService {

    @Autowired
    private RecipeDao recipeDao;

    @Autowired
    private RecipeNeedsDao needsDao;

    @Autowired
    private MaterialService materialService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String RECIPE_REPORT_SQL = "SELECT b.name as material_name,b.last_enter_price as material_price,m.material_weight,round(m.material_weight*b.last_enter_price,2) as material_cost,m.material_pct FROM yangzhou_sysw.t_recipe a left join t_recipe_more m on a.id = m.recipe_id left join t_material b on b.id = m.material_id where a.id=?";


    public List<RecipeDto> findAllRecipeDtos(){
        List<RecipeDto> results = Lists.newArrayList();
        for(Iterator<Recipe> iter = this.recipeDao.findAll().iterator();iter.hasNext();){
            Recipe recipe = iter.next();
            RecipeDto dto = new RecipeDto();
            dto.setId(recipe.getId());
            dto.setName(recipe.getRecipeName());
            dto.setCreateUser(recipe.getCreateUser().getName());
            if(recipe.getCreateTime()!=null){
                dto.setCreateTime(DateUtils.formatDateTime(recipe.getCreateTime()));
            }
            dto.setRecipeCost(recipe.getRecipeCost());
            dto.setTotalProduction(recipe.getTotalProduction());
            results.add(dto);
        }
        return results;
    }


    public RecipeNeeds findOne(Long id){
        return this.needsDao.findOne(id);
    }


    public Recipe findOneRecipe(Long id){
        return this.recipeDao.findOne(id);
    }

    /**
     * 配方包含多种原料
     * @param id
     * @return
     */
    public RecipeDto findOneRecipeDto(Long id){
        RecipeDto dto = new RecipeDto();
        Recipe recipe = this.findOneRecipe(id);
        if(recipe!=null){
            dto.setId(recipe.getId());
            dto.setName(recipe.getRecipeName());
            dto.setCreateUser(recipe.getCreateUser().getName());
            dto.setCreateTime(DateUtils.formatDate(recipe.getCreateTime()));
            dto.setRecipeCost(recipe.getRecipeCost());
        }
        return dto;
    }

    public Map<String, Object> findNutritionNeeds(RecipeNeeds needs){
        Map<String, Object> results = Maps.newHashMap();
        results.put("basic_dto", this.getNeedsDto(needs));
        if(needs.getTemplateMaterial() != null){
            results.put("nutri_common_dto",materialService.findNutriCommonByMaterial(needs.getTemplateMaterial()));
        }
        return results;
    }


    public RecipeNeedsDto getNeedsDto(RecipeNeeds needs){
        RecipeNeedsDto dto = new RecipeNeedsDto();
        dto.setId(needs.getId());
        dto.setStage(needs.getStage());
        dto.setUseObject(needs.getUseObject());
        dto.setStatus_value(needs.getStatus()>0?Constant.RECIPE_STATUS_1:Constant.RECIPE_STATUS_0);
        dto.setStatus(needs.getStatus());
        dto.setName(needs.getName());
        return dto;
    }

    public List<RecipeNeedsDto> findAllNeedsDto() {
        List<RecipeNeedsDto> dtoes = Lists.newArrayList();
        try {
            // 默认按照ID排序
            Sort sort = new Sort(Sort.Direction.ASC,"id");
            for (Iterator<RecipeNeeds> needs = this.needsDao.findAll(sort).iterator(); needs.hasNext(); ) {
                RecipeNeeds need = needs.next();
                RecipeNeedsDto dto = new RecipeNeedsDto();
                if (need != null) {
                    dto.setId(need.getId());
                    dto.setName(need.getName());
                    if (need.getCreateTime() != null) {
                        dto.setCreateTime(DateUtils.formatDateTime(need.getCreateTime()));
                    }
                    dto.setRecipeMarks(need.getRecipeMarks());
                    dto.setRecipeSource(need.getRecipeSource());
                    if (need.getCreateUser() != null) {
                        dto.setCreateUser(need.getCreateUser().getName());
                    }
                    dto.setStatus(need.getStatus());
                    dto.setStatus_value(need.getStatus() > 0 ? Constant.RECIPE_STATUS_1 : Constant.RECIPE_STATUS_0);
                    dto.setUseObject(need.getUseObject());
                    dto.setStage(need.getStage());
                }
                dtoes.add(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtoes;
    }


    public List<Map<String, Object>> queryRecipeReport(Long id){
        return jdbcTemplate.queryForList(RECIPE_REPORT_SQL,new Object[]{id});
    }
}
